<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\CachePayerScale;
use App\Http\Models\Business\CachePayerZ1Scale;
use App\Http\Models\Business\Payer;

class PayerReportPayerZ1Scale extends Payer
{

    //基础数据
    public $payer_scale_array = [];

    //总规模
    public $total_scale = ['quantity' => 0, 'amount' => 0, 'normal_amount' => 0];

    //结果数据， 键名为 品类id, 键值为数组，['quantity' => 0, 'amount' => 0, 'normal_amount' => 0]
    public $payer_scale = [];

    public function addPayerScale(CachePayerZ1Scale $model)
    {
        $this->payer_scale_array[] = $model;
    }


    public function setPayerZ1ScaleResultForSearch()
    {
        foreach ($this->payer_scale_array as $payer_scale) {
            $this->total_scale['quantity'] += $payer_scale->quantity;
            $this->total_scale['amount'] += $payer_scale->amount;
            $this->total_scale['normal_amount'] += $payer_scale->normal_amount;

            if (array_key_exists($payer_scale->category_contract_id, $this->payer_scale)) {
                $this->payer_scale[$payer_scale->category_contract_id]['quantity'] += $payer_scale->quantity;
                $this->payer_scale[$payer_scale->category_contract_id]['amount'] += $payer_scale->amount;
                $this->payer_scale[$payer_scale->category_contract_id]['normal_amount'] += $payer_scale->normal_amount;
            } else {
                $this->payer_scale[$payer_scale->category_contract_id]['quantity'] = $payer_scale->quantity;
                $this->payer_scale[$payer_scale->category_contract_id]['amount'] = $payer_scale->amount;
                $this->payer_scale[$payer_scale->category_contract_id]['normal_amount'] = $payer_scale->normal_amount;
            }
        }
    }

    public function getPayerScaleResult(int $category_contract_id, string $data_name)
    {
        $result = 0;
        if (isset($this->payer_scale[$category_contract_id]) && isset($this->payer_scale[$category_contract_id][$data_name])) {
            $result = $this->payer_scale[$category_contract_id][$data_name];
        }
        return $result;
    }



    public function getPayerScaleProportion(int $category_contract_id)
    {
        $result = 0;
        $current_category_amount = 0;
        if (isset($this->payer_scale[$category_contract_id])) {
            $current_category_amount = $this->payer_scale[$category_contract_id];
        }
        if ($this->total_scale_amount != 0) {
            $result = $current_category_amount / $this->total_scale_amount;
        }
        return round_percent($result);
    }
}
